package org.dreamwork.seagragon;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2010-4-24
 * Time: 18:21:35
 */
public class SeaGragonClassLoader extends URLClassLoader {
    private StringBuilder builder = new StringBuilder ();

    public SeaGragonClassLoader (File basedir) throws IOException {
        super (new URL [0], SeaGragonClassLoader.class.getClassLoader ());

        File classes = new File (basedir, "WEB-INF/classes");
        addURL (new File (classes.getCanonicalPath ()).toURI ().toURL ());
        builder.append (classes.getCanonicalPath ()).append (File.separatorChar);

        File libs = new File (basedir, "WEB-INF/lib");
        File[] jars = libs.listFiles (new FileFilter() {
            public boolean accept (File pathname) {
                String name = pathname.getName ().toLowerCase ();
                return name.endsWith (".jar") || name.endsWith (".zip");
            }
        });
        for (File jar : jars) {
            addURL (jar.toURI ().toURL ());
            builder.append (File.pathSeparatorChar).append (jar.getAbsoluteFile ());
        }
    }

    public String getClassPath () {
        return builder.toString ();
    }
}
